﻿using System;
using System.Collections.Generic;
using System.Text;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.IBatisRepository;
using OneMG.Phoenix.Respository;
using System.Data;
using OneMG.Phoenix.Entity;
using System.Collections;

namespace OneMG.Phoenix.Service
{
    public class NewsService : INewsService
    {
		 INews news = new NewsRepository();
         ICategory category = new CategoryRepository();
		 
        public System.Int32 AddNews(NewsModel model)
        {
			ENews entity = ConvertModelToEntity(model);
			
            return news.AddNews(entity);
        }

		public void UpdateByNewsID (NewsModel model)
		{
			ENews entity = ConvertModelToEntity(model);

            news.UpdateByNewsID(entity);

		}

		public void DeleteByNewsID (System.Int32 newsID)
		{
		
            news.DeleteByNewsID(newsID);

		}
		 
        public NewsModel GetByNewsID (System.Int32 newsID)
        {
        
			ENews entity = news.GetByNewsID(newsID);
			
            NewsModel model = ConvertEntityToModel(entity);


            return model;
        }

        public IList<NewsModel> GetPagedList(int pageSize, int pageIndex, int categoryID,out int totalRecord)
        {
            IList<ENews> entityList = news.GetPagedList(pageSize, pageIndex,  categoryID,out totalRecord);

            //约定1 为新闻分类
            IList<ECategory> categoryList = category.GetByParentID(1);

            Hashtable ht = new Hashtable();
            foreach (var item in categoryList)
            {
                ht.Add(item.CategoryID, item.CategoryName);
            }

           if (entityList == null)
               return null;

           IList<NewsModel> modelList = new List<NewsModel>();

           foreach (var item in entityList)
           {
               NewsModel model = ConvertEntityToModel(item);
               if (model != null)
               {
                   model.CategoryName = ht[model.CateID].ToString();
                   modelList.Add(model);
               }
           }
           return modelList;


        }
 

		public NewsModel ConvertEntityToModel(ENews entity)
		{
			if (entity == null)
            {
                return null;
            }
			NewsModel model = new NewsModel();
                        model.NewsID = entity.NewsID;
                        model.CateID = entity.CateID;
                        model.Title = entity.Title;
                        model.Summary = entity.Summary;
                        model.Description = entity.Description;
                        model.IsTop = entity.IsTop;
                        model.IsShow = entity.IsShow;
                        model.Visitation = entity.Visitation;
                        model.PublishTime = entity.PublishTime;
                        model.UpdateTime = entity.UpdateTime;
                        model.CreateUser = entity.CreateUser;
                        model.UpdateUser = entity.UpdateUser;
                        return model;
		}
		
		public ENews ConvertModelToEntity(NewsModel model)
		{
			if (model == null)
            {
                return null;
            }
			ENews entity = new ENews();
                        entity.NewsID = model.NewsID;
                        entity.CateID = model.CateID;
                        entity.Title = model.Title;
                        entity.Summary = model.Summary;
                        entity.Description = model.Description;
                        entity.IsTop = model.IsTop;
                        entity.IsShow = model.IsShow;
                        entity.Visitation = model.Visitation;
                        entity.PublishTime = model.PublishTime;
                        entity.UpdateTime = model.UpdateTime;
                        entity.CreateUser = model.CreateUser;
                        entity.UpdateUser = model.UpdateUser;
                        return entity;
		}
    }
}